<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="75px">
      <el-form-item label="设备名称" prop="deviceName">
        <el-input v-model="queryParams.deviceName" placeholder="请输入设备名称" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="设备类型" prop="deviceType">
        <el-select v-model="queryParams.deviceType" placeholder="设备类型" clearable style="width: 240px">
          <el-option v-for="dict in dict.type.device_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="设备状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="设备状态" clearable style="width: 240px">
          <el-option v-for="dict in dict.type.status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属风场" prop="windFarmId">
        <el-select v-model="queryParams.windFarmId" placeholder="所属风场" clearable style="width: 240px">
          <el-option v-for="device in orgListData" :key="device.windFarmId" :label="device.orgName" :value="device.windFarmId" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否大屏" prop="isShowFlag">
        <el-select v-model="queryParams.isShowFlag" placeholder="是否大屏" clearable style="width: 240px">
          <el-option v-for="dict in dict.type.is_show_flag" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:shebei:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:shebei:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:shebei:remove']">删除</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="typeList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" width="80" align="center">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="设备名称" align="center" prop="deviceName" :show-overflow-tooltip="true" />
      <el-table-column label="设备类型" align="center" prop="deviceType" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_type" :value="scope.row.deviceType" />
        </template>
      </el-table-column>
      <el-table-column label="设备状态" align="center" prop="status" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="所属风场" align="center" prop="orgName">
      </el-table-column>
      <el-table-column label="是否展示大屏" align="center" prop="isShowFlag" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_show_flag" :value="scope.row.isShowFlag" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:shebei:edit']">修改</el-button>
          <el-button size="mini" v-if="scope.row.status!='9'" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:shebei:remove']">失效</el-button>
          <el-button size="mini" v-else type="text" icon="el-icon-plus" @click="handleHy(scope.row)"
            v-hasPermi="['system:shebei:hy']">还原</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="98px">
        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="设备类型" prop="deviceType">
          <el-select v-model="form.deviceType" placeholder="设备类型" clearable style="width: 100%">
            <el-option v-for="dict in dict.type.device_type" :key="dict.value" :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="设备状态" prop="status">
          <el-select v-model="form.status" placeholder="设备状态" clearable style="width: 100%">
            <el-option v-for="dict in dict.type.status" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="视频地址" prop="rtspUrl">
          <el-input v-model="form.rtspUrl" placeholder="请输入视频地址" />
        </el-form-item>
        <el-form-item label="所属风场" prop="windFarmId">
          <el-select v-model="form.windFarmId" placeholder="所属风场" clearable style="width: 100%">
            <el-option v-for="device in orgListData" :key="device.windFarmId" :label="device.orgName"
              :value="device.windFarmId" />
          </el-select>
        </el-form-item>
        <el-form-item label="是否大屏" prop="isShowFlag">
          <el-select v-model="form.isShowFlag" placeholder="是否大屏" clearable style="width: 100%">
            <el-option v-for="dict in dict.type.is_show_flag" :key="dict.value" :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="设备序列号" prop="serialNumber">
          <el-input v-model="form.serialNumber" placeholder="请输入设备序列号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listType, getType, delType, addType, updateType, findOrgList,hyType } from "@/api/system/shebei"

export default {
  name: "device",
  dicts: ['device_type', 'is_show_flag', 'status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 所属风场下拉
      orgListData: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceName: undefined,
        deviceType: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deviceName: [
          { required: true, message: "设备名称不能为空", trigger: "blur" }
        ],
        deviceType: [
          { required: true, message: "设备类型不能为空", trigger: "blur" }
        ],
        windFarmId: [
          { required: true, message: "所属风场不能为空", trigger: "blur" }
        ],
        rtspUrl: [
          { required: true, message: "视频地址不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "设备状态不能为空", trigger: "blur" }
        ],
        isShowFlag: [
          { required: true, message: "是否大屏不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.orgList()
  },
  methods: {
    /** 查询设备类型列表 */
    getList() {
      this.loading = true
      listType(this.addDateRange(this.queryParams, this.dateRange)).then(response => {

        this.typeList = response.rows
        this.total = response.total
        this.loading = false
      }
      )
    },
    /** 查询所属风场编码集合 */
    orgList() {
      this.loading = true
      findOrgList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {

        this.orgListData = response.data
      }
      )
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        deviceId: undefined,
        deviceName: undefined,
        deviceType: undefined,
        status: "0",
        remark: undefined
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加设备类型"
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deviceId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const deviceId = row.deviceId || this.ids
      getType(deviceId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改设备类型"
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 找到选择的windFarmId对应的orgName
          const selectedOrg = this.orgListData.find(item => item.windFarmId === this.form.windFarmId);
          if (selectedOrg) {
            // 将orgName添加到表单数据中
            this.form.orgName = selectedOrg.orgName;
          }

          if (this.form.deviceId != undefined) {
            updateType(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addType(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const deviceIds = row.deviceId || this.ids
      this.$modal.confirm('是否确认失效？').then(function () {
        return delType(deviceIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("失效成功")
      }).catch(() => { })
    },
    /** 还原按钮操作 */
    handleHy(row) {
      const deviceIds = row.deviceId || this.ids
      this.$modal.confirm('是否确认还原？').then(function () {
        return hyType(deviceIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("还原成功")
      }).catch(() => { })
    }
  }
}
</script>
